package com.tools.web.work.excel.poi;

import com.tools.common.object.Note;
import org.apache.poi.ss.usermodel.Row;

import java.util.Objects;

/**
 * 读取 Excel 过程中出错的记录信息类
 * */
@Note("读取 Excel 过程中出错的记录信息类")
public class ErrorMsg {

    @Note("错误的位置。如第几个 Sheet 页的第几行第几列")
    private String location;

    @Note("错误的具体信息")
    private String message;

    @Note("导出时 - 默认的表头的配置")
    private static ExcelHead DEFAULT_HEAD;

    /* ************************************************************************************************************
     *
     *      构造器
     *
     * ************************************************************************************************************
     * */

    public ErrorMsg() {}

    public ErrorMsg(String location, String message) {
        this.setLocation(location);
        this.setMessage(message);
    }

    public static ErrorMsg of() {
        return new ErrorMsg();
    }

    public static ErrorMsg of(String location, String message) {
        return new ErrorMsg(location, message);
    }

    /* ************************************************************************************************************
     *
     *      Getter / Setter
     *
     * ************************************************************************************************************
     * */

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public void setLocation(int sheetIndex, int rowIndex, int columnIndex) {
        this.location = "第 " + sheetIndex + " 页第 " + rowIndex + " 行第 " + columnIndex + " 列";
    }

    public void setLocation(int sheetIndex, Row row, int columnIndex) {
        this.location = "第 " + sheetIndex + " 页第 " + row.getRowNum() + " 行第 " + columnIndex + " 列";
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    @Override
    public String toString() {
        return "ReadErrorMsg{" +
                "location='" + location + '\'' +
                ", message='" + message + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ErrorMsg that = (ErrorMsg) o;
        return Objects.equals(location, that.location) && Objects.equals(message, that.message);
    }

    @Override
    public int hashCode() {
        return Objects.hash(location, message);
    }

    /* ************************************************************************************************************
     *
     *      链式调用
     *
     * ************************************************************************************************************
     * */

    public ErrorMsg location(String location) {
        this.setLocation(location);
        return this;
    }

    public ErrorMsg location(int sheetIndex, int rowIndex, int columnIndex) {
        this.setLocation(sheetIndex, rowIndex, columnIndex);
        return this;
    }

    public ErrorMsg location(int sheetIndex, Row row, int columnIndex) {
        this.setLocation(sheetIndex, row, columnIndex);
        return this;
    }

    public ErrorMsg message(String message) {
        this.setMessage(message);
        return this;
    }

    @Note("获取默认的表头配置 - 导入时的错误信息")
    public static ExcelHead getDefaultHead() {
        if(DEFAULT_HEAD != null) return DEFAULT_HEAD;
        synchronized (ErrorMsg.class) {
            if(DEFAULT_HEAD != null) return DEFAULT_HEAD;
            DEFAULT_HEAD = new ExcelHead()
                    .add("location", "错误位置")
                    .add( "message", "错误信息");
            return DEFAULT_HEAD;
        }
    }



}
